//
//  MSRequsetClient.m
//  MSJD
//
//  Created by xiangbo on 2018/9/16.
//  Copyright © 2018 xiangbo. All rights reserved.
//

#import "MSRequsetClient.h"

@interface MSRequsetClient ()

@end

@implementation MSRequsetClient

+(instancetype)shareInstance{
    static MSRequsetClient *client = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        client = [[MSRequsetClient alloc] init];
        client.baseUrl = @"http://47.107.74.24";
    });
    return client;
}

- (void)getHomePageInfo:(void (^)(BOOL, MSResponseData * _Nonnull))completion{
    NSString *url = @"v1/index";
    [self GET:url params:nil classType:MSResponse.class completion:^(NSURLSessionDataTask *task, id response, NSError *error) {
        [MSRequsetClient dealForResponse:response classType:MSResponseData.class completion:completion];
    }];
}

- (void)getFoodPageInfo:(NSNumber *)kid completion:(void (^)(BOOL, MSFoodData * _Nonnull))completion{
    NSString *url = @"v1/food/index";
    NSDictionary *params = @{@"kid":kid ?: @"",@"lon":@"", @"lat":@""};
    [self GET:url params:params classType:MSFoodResponse.class completion:^(NSURLSessionDataTask *task, id response, NSError *error) {
        [MSRequsetClient dealForResponse:response classType:MSFoodData.class completion:completion];
    }];
}

- (void)getFilterList:(NSNumber *)kindId completion:(void (^)(BOOL, MSListData * _Nonnull))completion{
    NSString *url = @"v1/item/filters";
    NSDictionary *params = @{@"kindId":kindId ?: @""};
    [self GET:url params:params classType:MSListResponse.class completion:^(NSURLSessionDataTask *task, id response, NSError *error) {
        [MSRequsetClient dealForResponse:response classType:MSListData.class completion:completion];
    }];
}

- (void)getFoodSearchResult:(NSDictionary *)params completion:(void (^)(BOOL, MSFoodSearchData * _Nonnull))completion{
    NSString *url = @"v1/food/search";
    [self GET:url params:params classType:MSFoodSearchResponse.class completion:^(NSURLSessionDataTask *task, id response, NSError *error) {
        [MSRequsetClient dealForResponse:response classType:MSFoodSearchData.class completion:completion];
    }];
}

- (void)getRestaurantDetail:(NSNumber *)rId completion:(void (^)(BOOL, MSFoodDetailData * _Nonnull))completion{
    NSString *url = @"v1/food/detail";
    NSDictionary *params = @{@"id":rId ?: @""};
    [self GET:url params:params classType:MSFoodDetailResponse.class completion:^(NSURLSessionDataTask *task, id response, NSError *error) {
        [MSRequsetClient dealForResponse:response classType:MSFoodDetailData.class completion:completion];
    }];
}

- (void)getRestaurantCommentList:(NSNumber *)rId page:(NSNumber *)page size:(NSNumber *)size completion:(void (^)(BOOL, MSCommentlistData * _Nonnull))completion{
    NSString *url = @"v1/food/detail";
    NSDictionary *params = @{@"id":rId ?: @"",@"page" : page ?: @(0), @"size" : size ?: @(20)};
    [self GET:url params:params classType:MSCommentlistResponse.class completion:^(NSURLSessionDataTask *task, id response, NSError *error) {
        [MSRequsetClient dealForResponse:response classType:MSCommentlistData.class completion:completion];
    }];
}


- (void)publishComment:(NSDictionary *)params completion:(void (^)(BOOL))completion{
    NSString *url = @"v1/comment/publish";
    NSString *resuestUrl = [self getRequstUrl:url];
    [MSRequestOperation POST:resuestUrl params:params headers:@{@"Content-Type" : @"application/x-www-form-urlencoded"} classType:MappedModel.class completion:^(NSURLSessionDataTask *task, id response, NSError *error) {
        if (completion) {
            completion(((MappedModel *)response).code == 200);
        }
    }];
    
}
#pragma mark - base or private Method

- (void)GET:(NSString *)url params:(NSDictionary *)params classType:(Class)classType completion:(MSCompletionBlock)completion {
    NSString *requstUrl = [self getRequstUrl:url];
    NSMutableDictionary *commonParams = [MSRequsetClient getCommonDict];
    if (params) {
        [commonParams addEntriesFromDictionary:params];
    }
    [MSRequestOperation GET:requstUrl params:commonParams classType:classType completion:completion];
}

- (void)POST:(NSString *)url params:(NSDictionary *)params classType:(Class)classType completion:(MSCompletionBlock)completion {
    NSString *requstUrl = [self getRequstUrl:url];
    NSMutableDictionary *commonParams = [MSRequsetClient getCommonDict];
    if (params) {
        [commonParams addEntriesFromDictionary:params];
    }
    [MSRequestOperation POST:requstUrl params:params headers:commonParams classType:classType completion:completion];
}

- (NSString *)getRequstUrl:(NSString *)url{
    return [NSString stringWithFormat:@"%@%@",self.baseUrl,url];
}

+ (void)dealForResponse:(id)response classType:(Class)classType completion:(void(^)(BOOL,id))completion{
    BOOL ret = NO;
    id data = nil;
    if (response) {
        MappedModel *mappedResponse = response;
        ret = mappedResponse.code == 200;
        if ([response respondsToSelector:@selector(data)]) {
            data = [response data];
        }
        if (![data isKindOfClass:classType]) {
            data = nil;
        }
    }
    if (completion) {
        completion(ret,data);
    }
}

+ (NSMutableDictionary *)getCommonDict{
    return @{@"uuid":@"7859026435b7454abbef35225c7d01a0",
             @"token":@"7859026435b7454abbef",
             @"did":[OpenUDID value],
             @"os":@"ios",
             @"osVersion": MS_SDK_VERSION_STRING ?: @"",
             @"appVersion": MS_APP_VERSION ?: @"",
             @"device":[DeviceInfo getDeviceType] ?: @"",
             @"seqid":[[NSUUID UUID] UUIDString] ?: @""}.mutableCopy;
}
@end
